#include "hash_table.h"

template <typename HashedObj>
HashTable<HashedObj>::HashTable(int size)
	:array(size) 
{
	makeEmpty();
}

template <typename HashedObj>
void HashTable<HashedObj>::makeEmpty()
{
	current_size=0;
	for(unsigned i=0;i<array.size(); i++)
		array[i].info=EMPTY;
}

template <typename HashedObj>
bool HashTable<HashedObj>::contains(const HashedObj &x) const
{
	return isActive(findPos(x));
}

template <typename HashedObj>
int HashTable<HashedObj>::findPos(const HashedObj & x) const
{
	int offset=1;
	int currentPos=myhash(x);
	
	while(array[currentPos].info != EMPTY &&
		  array[currentPos].element != x)
	{
		currentPos += offset;
		offset += 2;
		if(currentPos >= array.size() )
			currentPos -= array.size ();
	}
	return currentPos;
}
template <typename HashedObj>
bool HashTable<HashedObj>::insert(const HashedObj &x )
{
		int currentPos=findPos(x);
		if(isActive(currentPos))
			return false;
		
		array[currentPos]=HashEntry(x,ACTIVE);
		
		if(++current_size>array.size()/2)
			rehash();
			
		return true;
}

template <typename HashedObj>
bool HashTable<HashedObj>::remove(const HashedObj &x )
{
	int currentPos=findPos(x);
	if(!isActive(currentPos))
		return false;
	
	array[currentPos].info =  DELETED;
	return true;
}

template <>
int HashTable<int>::myhash(const int& x) const
{
	return x%array.size();
}
